package SwordFingerOffer;

public class Q18_DeleteListNode {
    //删除链表节点

    public static ListNode deleteNode(ListNode head, int val) {
        if (head == null){
            return null;
        }

        if (head.val == val && head.next != null){
            return head.next;
        }
        if (head.val == val){
            return null;
        }
        //快慢指针
        ListNode pre = head;
        ListNode now = head.next;
        while (now != null){
            if (now.val == val){
                //删除操作
                pre.next = now.next;
            }
            now = now.next;
            pre = pre.next;
        }

        return head;
    }

    public static void main(String[] args) {
        // 4519
        ListNode head = new ListNode(4);
        head.next = new ListNode(5);
        head.next.next = new ListNode(1);
        head.next.next.next = new ListNode(9);
        System.out.println(deleteNode(head, 5));
    }
}
